<?php


namespace App\Admin\Services;


use App\Commons\Utils\AliYunCsTool;

class UploadImgService {
    /**
     * 默认文件提交参数名
     */
    const DEFAULT_FILE_KEY_NAME = 'img';
    
    /**
     * 通用文件目录
     */
    const COMMON_FILE_DIR = 'file';
    
    /**
     * url路径分割符号
     */
    const DIRECTORY_SEPARATOR = '/';
    
    /**
     * @return null
     * @throws \App\Commons\Exceptions\AliyunCsException
     * @throws \OSS\Core\OssException
     */
    public static function storeImgToAliOss() {
        $request     = request();
        $fileKeyName = $request->input('fileKeyName') ?: self::DEFAULT_FILE_KEY_NAME;
        
        return AliYunCsTool::commonUpload(
            $request->file($fileKeyName),
            null,
            AliYunCsTool::OBJECT_PREFIX_COM . self::DEFAULT_FILE_KEY_NAME . self::DIRECTORY_SEPARATOR
        );
    }
    
    /**
     * @return null
     * @throws \App\Commons\Exceptions\AliyunCsException
     * @throws \OSS\Core\OssException
     */
    public static function storeFileToAliOss() {
        $request     = request();
        $fileKeyName = $request->input('fileKeyName') ?: self::COMMON_FILE_DIR;
        
        return AliYunCsTool::commonUpload(
            $request->file($fileKeyName),
            null,
            AliYunCsTool::OBJECT_PREFIX_COM . self::COMMON_FILE_DIR . self::DIRECTORY_SEPARATOR
        );
    }
}